Smalltalk current createPackage: 'Easnoth-Core' properties: #{}!
Object subclass: #Cell
	instanceVariableNames: 'drawedObjects x y'
	package: 'Easnoth-Core'!

!Cell methodsFor: 'accessing'!

drawedObjects
	^drawedObjects
!

drawedObjects: value
	drawedObjects := value.
!

tile
	^drawedObjects at: 1
!

tile: aTile
	drawedObjects at: 1 put: aTile.
!

x
	^x
!

x: value
	x := value.
!

y
	^y
!

y: value
	y := value.
! !

!Cell methodsFor: 'displaying'!

draw
	drawedObjects do: [ :drawedObject |
		drawedObject ifNotNil: [drawedObject drawX: self x y: self y]].
! !

!Cell class methodsFor: 'instance creation'!

newWithContext: aContext cellArray: aCellArray coods: coods
	^self new
		drawedObjects: (Array new: 8);
		x: (coods at: 1);
		y: (coods at: 2);
		tile: (Tile newWithContext: aContext index: (aCellArray at: 1));
		yourself.
! !

Object subclass: #DrawedObject
	instanceVariableNames: 'image context'
	package: 'Easnoth-Core'!

!DrawedObject methodsFor: 'accessing'!

context
	^context
!

context: aCtx
	context := aCtx
!

image
	^image
!

image: anImage
	image := anImage
!

initializeContext: aCtx index: anIndex
	self context: aCtx.
	self image: (self class arrayImg atIndex: anIndex). "TODO use library"
! !

!DrawedObject methodsFor: 'displaying'!

displayX: aX y: aY
	self context drawImage: self image
						x: aX
						y: aY.
!

drawX: aX y: aY
	self subclassResponsibility
! !

DrawedObject class instanceVariableNames: 'arrayImg'!

!DrawedObject class methodsFor: 'accessing'!

arrayImg
	^arrayImg
!

arrayImg: anArray
	arrayImg := anArray
! !

DrawedObject subclass: #Ground
	instanceVariableNames: ''
	package: 'Easnoth-Core'!

!Ground methodsFor: 'displaying'!

drawX: aX y: aY
	"draw through isometric transformation"

	|ctx|
	ctx := self context.
	ctx save;
		translate: aX + 45 y: aY-7;
		scale: 1 y: 0.5;
		rotate: Number pi / 4 ;
		scale: 1.4 y: 1.23.
	self displayX: 0 y: 0.
	ctx restore.
! !

Ground subclass: #OverTile
	instanceVariableNames: ''
	package: 'Easnoth-Core'!

!OverTile class methodsFor: 'instance creation'!

newWithContext: aContext index: anIndex
	^self new
		initializeContext: aContext index: anIndex;
		yourself.
! !

Ground subclass: #Tile
	instanceVariableNames: 'isWall'
	package: 'Easnoth-Core'!

!Tile methodsFor: 'accessing'!

isWall
	^isWall
!

isWall: aBoolean
	isWall := aBoolean
! !

!Tile class methodsFor: 'instance creation'!

newWithContext: aContext index: anIndex
	^self new
		initializeContext: aContext index: anIndex;
		isWall: false;
		yourself.
! !

DrawedObject subclass: #OverGround
	instanceVariableNames: 'hp'
	package: 'Easnoth-Core'!

!OverGround methodsFor: 'accessing'!

hp
	^hp
!

hp: aNumber
	hp := aNumber
! !

OverGround subclass: #Building
	instanceVariableNames: ''
	package: 'Easnoth-Core'!

OverGround subclass: #Monster
	instanceVariableNames: ''
	package: 'Easnoth-Core'!

Object subclass: #Map
	instanceVariableNames: 'canvas width height mapX mapY cells'
	package: 'Easnoth-Core'!

!Map methodsFor: 'accessing'!

canvas
	^canvas
!

cells
	^cells
!

cells: anArray
	cells := anArray.
!

height
	^height
!

height: aLength
	height := aLength
!

mapX
	^mapX
!

mapX: aLength
	mapX := aLength
!

mapY
	^mapY
!

mapY: aLength
	mapY := aLength
!

width
	^width
!

width: aLength
	width := aLength
! !

!Map methodsFor: 'displaying'!

update
	(self canvas element getContext: '2d') clearRect: 0 y: 0 width: 1366 height: 768.
	self cells do: [:row |
		row do: [ :cell |
			cell draw.
		]
	]
! !

!Map methodsFor: 'initialize-release'!

canvasId
	^'#main'
!

canvasPadding
	^40
!

initialize
	super initialize.
	canvas := TagBrush new.
	self canvas initializeFromJQuery: self canvasId asJQuery canvas: (HTMLCanvas onJQuery: 'body' asJQuery).
!

initializeCellsWithMapArray: aMapArray
	"TODO put this cste in config file"
	|sin45 sin15 sin135 sin75 cell xpos ypos temp cood|
	sin45 := 0.707106781. "Pi/4"
	sin15 := 0.258819045. "Pi/12"
	sin135 := 0.233445364. "???->I used Approximation"
	sin75 := 0.965925826. "5*Pi/12"
	
	aMapArray withIndexDo: [:row :i |
		row withIndexDo: [ :cellArray :j |
			temp := j + ((i + 1)/2) truncated.
			xpos := (i * (sin45 + sin75) - (temp* (sin15 + sin75))) * self tileUnit + self mapX.
			ypos := (temp * (sin15 + sin75) / 2 + (i * sin135)) * self tileUnit + self mapY.
			cood := Array new: 2.
			cood at: 1 put: xpos.
			cood at: 2 put: ypos.
			(self cells at: i) at: j put: (Cell newWithContext: (self canvas element getContext: '2d') cellArray: cellArray coods: cood).		
		]
	]
!

initializeWithMapArray: aMapArray
	self width: aMapArray size.
	self height: (aMapArray at:1) size.
	self mapX: self canvasPadding + (self height * self tileUnit * 1.17).
	self mapY: self canvasPadding.
	self cells: (Array new: self width).
	1 to: self width do: [:i | 
			self cells at: i put: (Array new: self height)].
	self initializeCellsWithMapArray: aMapArray.
!

tileUnit
	^50
! !

!Map class methodsFor: 'instance creation'!

newWithMapArray: aMapArray 
	^self new initializeWithMapArray: aMapArray
! !

